package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.annotation.CacheFind;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import com.jt.util.ObjectMapperUtil;
import com.jt.vo.EasyUITree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ItemCatMapper itemCatMapper;
    //商品查询页面的叶子目录 :也就是分类
    @Autowired(required = false)//类似于懒加载
    private Jedis jedis;

    @Override
    @CacheFind(key = "ITEM_CAT")
    public ItemCat findItemCatName(Long itemCatId) {
        ItemCat itemCat = itemCatMapper.selectById(itemCatId);
        return itemCat;
    }

    //新增商品时候的父级元素tree
    @Override
    @CacheFind(key = "ITEM_CAT_PARENT" ,seconds=7*24*60*60)
    public List<EasyUITree> findItemCatList(long parentId) {
        List<EasyUITree> easyUITrees = new ArrayList<>();
        //暂时只查询一级商品分类信息
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        //数据库记录 List<ItemCat>对象   pojo对象
        List<ItemCat> iteList = itemCatMapper.selectList(queryWrapper);
        // 页面中要的数据List<EasyUITree>  VO对象
        /**
         *  private long id; //节点id
         *     private String text; //定义名称
         *     private String state; //节点状态
         *
         *
         */
        for (ItemCat itemCat : iteList) {
            long id = itemCat.getId();
            String text = itemCat.getName();
            String state = itemCat.getIsParent() ? "closed" : "open";
            EasyUITree easyUITree = new EasyUITree(id, text, state);
            easyUITrees.add(easyUITree);
        }

        // 需要将ItemCat对象转化为EasyUITree对象  get/set...
        return easyUITrees;
    }

    @Override
    public List<EasyUITree> findItemCatCache(Long parentId) {
        Long startTime = System.currentTimeMillis();
        List<EasyUITree> easyUITrees = new ArrayList<>();
        String key = "ITEM_CAT_PARENT::" + parentId;
        if (jedis.exists(key)) {
            //存在直接获取缓存对象
            String json = jedis.get(key);
            easyUITrees = ObjectMapperUtil.toObject(json, easyUITrees.getClass());
            System.out.println("查询redis缓存");
            long endTime = System.currentTimeMillis();
            System.out.println("耗时:"+(endTime-startTime)+"毫秒");
            //return easyUITrees;
        } else {
            easyUITrees = findItemCatList(parentId);
            String json = ObjectMapperUtil.toJson(easyUITrees);
            jedis.setex(key, 7 * 24 * 60 * 60, json);
            System.out.println("查询数据库获取结果");
            long endTime = System.currentTimeMillis();
            System.out.println("耗时:"+(endTime-startTime)+"毫秒");
        }
        //System.out.println(easyUITrees);
        return easyUITrees;
    }
}
